<template>
	<view class="address">
		<app-title title='收货地址'></app-title>


		<radio-group class="addressRadioGroup" @change="radioChange">
			<label class="list" v-for="item in res" :key="res.id" v-if="!isdel">
				<view class="cont">
					<view class="inner">
						<view class="titles">
							<text class="realname" v-cloak>{{item.realname}}</text>
							<text class="mobile" v-cloak>{{item.mobile}}</text>
						</view>
						<view class="text" v-cloak>
							{{item.province}}{{item.city}}{{item.area}}&nbsp;{{item.street}}&nbsp;{{item.address}}
						</view>
						<view class="bar clear">
							<view class="pull-right webkit flex">
								<view @click.stop="jump" :data-id="item.id">
									<image src="@/static/member/edit.png"></image>
									<text>编辑</text>
								</view>
								<view @click.stop='alertDialogOpen(item.id)' :data-id="item.id">
									<image src="@/static/member/delete.png"></image>
									<text>删除</text>
								</view>
							</view>
							<view class="radio-inline" :data-id="item.id">
								<radio :value="item.id" :checked="item.isdefault" color="#f55" />
								<text style="position: relative;top: 4upx;">设为默认</text>
							</view>
						</view>
					</view>
				</view>
			</label>
		</radio-group>


		<view class="footer">
			<view class="footer_box" style="background-color: #FF3B2F;" @click="jump">新增地址</view>
		</view>

		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="error" cancelText="取消" confirmText="确认" title="删除地址" content="确定要删除当前地址吗？"
				@confirm="del" @close="dialogClose"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		member_address,
		member_address_delete,
		member_address_setdefault
	} from '@/api/member.js'
	export default {
		data() {
			return {
				res: [],
				openid: '',
				isdel: false,
				delete: ''
			};
		},
		onLoad: function(e) {
			uni.showLoading()
			uni.getStorage({
				key: 'openid',
				success: (res) => {
					this.openid = res.data
					this.setAjax()
				}
			});
		},
		onPullDownRefresh() {
			this.setAjax();
		},
		onShow() {
			this.setAjax();
		},
		methods: {
			alertDialogOpen(e) {
				this.delete = e;
				this.$refs.alertDialog.open();
			},
			dialogClose() {
				this.$refs.alertDialog.close();
			},
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			async radioChange(e) {
				let up = {
					openid: this.openid,
					id: e.detail.value
				}
				let res = await member_address_setdefault(up);
				console.log(res);
			},
			async setAjax() {
				let up = {
					openid: this.openid
				}
				let res = await member_address(up);
				console.log(res);
				for (var i in res) {
					if (res[i].isdefault == 0) {
						res[i].isdefault = false
					} else {
						res[i].isdefault = true
					}
				}
				this.res = res;

			},
			jump(e) {
				// event.stopPropagation();
				uni.navigateTo({
					url: 'post?id=' + e.currentTarget.dataset.id
				});
			},
			async del(e) {
				let up = {
					openid: this.openid,
					id: this.delete
				}
				let data = await member_address_delete(up);
				if (data.status === 1) {
					uni.showToast({
						title: "删除成功"
					});
					this.setAjax();
				}
			},
			async moren(e) {
				let up = {
					openid: this.openid,
					id: e.currentTarget.dataset.id
				}
				let res = await member_address_setdefault(up);
				if (res.status === 1) {
					uni.showToast({
						title: "设置成功"
					})
				}
			}
		}
	}
</script>
<style lang="scss">
	.address {
		width: 100%;
		height: 100vh;
		display: flex;
		flex-direction: column;

	}

	.addressRadioGroup {
		width: 100%;
		flex: 1;
		overflow-y: auto;
		padding: 0 20rpx 130rpx 20rpx;
		box-sizing: border-box;
	}

	.list {
		padding: 0;
		display: block;
		padding-top: 10upx;
		box-shadow: 0 0 10rpx #ccc;
		margin: 30rpx 0;
		border-radius: 10rpx;
		overflow: hidden;
	}

	.cont {
		background: #fff;
	}

	.inner {
		padding: 16upx 24upx;
	}

	.titles {
		font-size: 28upx;
		color: #000;
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
		height: 40upx;
		line-height: 40upx;
	}

	.realname {
		margin-right: 10upx;
	}

	.text {
		font-size: 26upx;
		color: #666;
	}

	.bar {
		position: relative;
		color: #666;
		margin-top: 12upx;
		padding-top: 8upx;
		line-height: 56upx;
	}

	.bar:before {
		content: " ";
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 2upx;
		border-top: 2upx solid #ebebeb;
		color: #ebebeb;
		-webkit-transform-origin: 0 0;
		-ms-transform-origin: 0 0;
		transform-origin: 0 0;
		-webkit-transform: scaleY(0.5);
		-ms-transform: scaleY(0.5);
		transform: scaleY(0.5);
	}

	.pull-right {
		float: right;
		display: flex;
	}

	.pull-right view {
		margin-left: 20upx;
	}

	.pull-right view image {
		width: 34upx;
		height: 34upx;
		position: relative;
		top: 6upx;
	}

	.footer {
		position: fixed;
		bottom: 0rpx;
		width: 100%;
		z-index: 2000;
		padding: 20rpx 50rpx 20rpx 50rpx;
		box-sizing: border-box;
		background-color: #fff;

		.footer_box {
			width: 100%;
			height: 80rpx;
			border-radius: 80rpx;
			background: linear-gradient(3deg, #ff3232, #ff3232, #ff8719);
			text-align: center;
			line-height: 80rpx;
			color: #fff;
		}
	}

	.footer button {
		border-radius: 0;
		background: #f55;
	}

	.footer button.button-hover {
		background: #dc4d4d;
	}
</style>